package com.scc.persistence;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.scc.entites.DishEntity;

@Repository
public class AdvancedSearch implements IAdvancedSearch
{
   private SessionFactory sessionFactory;
   
   @SuppressWarnings("unchecked")
   public List<DishEntity> advancedSearch(LookupFilter filter)
   {
      Session session = getSession();
      Criteria crit = session.createCriteria(DishEntity.class);
      addParameters(crit, filter);
      
      List<DishEntity> list = crit.list();
      
      return list; 
   }
   
   private void addParameters(Criteria crit, LookupFilter filter)
   {
      if (filter.getName() != null)
      {
         crit.add(Restrictions.eq("name", filter.getName()));
      }
      if (filter.getWeight() != null)
      {
         crit.add(Restrictions.eq("weight", filter.getWeight()));
      }
      if (filter.getDescription() != null)
      {
         crit.add(Restrictions.eq("description", filter.getDescription()));
      }
   }
   
   private Session getSession()
   {
      return getSessionFactory().getCurrentSession();
   }
   
   public SessionFactory getSessionFactory()
   {
      return sessionFactory;
   }
   
   @Resource(name="sessionFactory")
   public void setSessionFactory(SessionFactory sessionFactory)
   {
      this.sessionFactory = sessionFactory;
   }
}
